# Maintainer: Oleg Titov <oleg.titov@gmail.com>

pkgbase=zstd
pkgname=('zstd' 'libzstd' 'libzstd-devel')
pkgver=1.4.4
pkgrel=2
pkgdesc="Zstandard - Fast real-time compression algorithm"
arch=('i686' 'x86_64')
url='https://facebook.github.io/zstd/'
license=('BSD')
makedepends=('gcc' 'make' 'cmake')
source=(${pkgbase}-${pkgver}.tar.gz::"https://github.com/facebook/${pkgname}/archive/v${pkgver}.tar.gz"
        'fix-core-count-detection.patch'
        'define-posix-c-source.patch')
sha256sums=('a364f5162c7d1a455cc915e8e3cf5f4bd8b75d09bc0f53965b0c9ca1383c52c8'
            '71f7c97fbc19554ce225a7e72ba658cf855e8492b980843581af13ffad8c52ca'
            '7ca0ca31df6099774ba2bba1b6f3cb7e376ae71c9e5775fb8602836231864416')
options=(!libtool)

prepare() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  patch -p1 -i ${srcdir}/fix-core-count-detection.patch
  patch -p1 -i ${srcdir}/define-posix-c-source.patch
}

build() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    ../${pkgbase}-${pkgver}/build/cmake

  make install DESTDIR="${srcdir}/dest"
}

package_zstd() {
  groups=('libraries')
  depends=('gcc-libs' 'libzstd')

  cd "${srcdir}/${pkgbase}-${pkgver}"
  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/man/
  rm -f ${pkgdir}/usr/bin/*.dll
  install -D -m644 "LICENSE" "${pkgdir}/usr/share/licenses/${pkgbase}/LICENSE"
}

package_libzstd() {
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libzstd-devel() {
  groups=('development')
  options=('staticlibs')
  depends=("libzstd=${pkgver}")
  pkgdesc="zstd headers and libraries"
  
  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
